/*
 * 文件描述：自定义进度条类sliderProgress的实现文件
 * 文件作用：实现自定义进度条类sliderProgress
 * 文件创建时间：2017-4-1
 * 文件创建者：wsgalaxy@163.com
 * 文件最后修改时间：
 * 文件修改者：
 * 文件协议：GPL
 *
 */


#include <QPainter>

#include "sliderProgress.h"

sliderProgress::sliderProgress(QWidget *parent) : QWidget(parent)
{
    (void) parent;

    this->setFixedHeight(50);
    this->setMinimumWidth(110);

    maximum = 100;
    minimum = 0;
    tracking = false;
    value = maximum;
    singleStep = 1;
    isPointed = 0;
    isAlwaysShow = 1;
    isShow = 1;
    allowSetValue = 1;

    this->setMouseTracking(true);

}


void sliderProgress::paintEvent(QPaintEvent *event)
{
    (void) event;

    double pos = value*(this->width()-20)/(maximum-minimum)+10;     //当前value下游标相对于整个控件的水平位置

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillRect(this->rect(), QColor(0, 0, 0, 0));

    QPen pen;
    pen.setColor(QColor(128, 128, 128, 128));
    pen.setWidth(10);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);

    painter.drawLine(QPointF(5+10, 25), QPointF(this->width()-5-10, 25));

    pen.setColor(QColor(0xEE, 0x22, 0x22, 0xEE));
    painter.setPen(pen);

    painter.drawLine(QPointF(5+10, 25), QPointF(value*(this->width()-20)/(maximum-minimum)-5+10, 25));


    if(isShow == 1)     //由isAlwaysShow控制是否始终显示游标
    {

        pen.setWidth(1);
        pen.setColor(QColor(0xFF, 0xFF, 0xFF, 0xEE));
        painter.setPen(pen);
        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(QColor(0xFF, 0xFF, 0xFF, 0xEE));
        painter.setBrush(brush);

        //painter.drawEllipse(pos-10, 15, 20, 20);
        painter.drawEllipse(QPointF(pos, 25), 10, 10);

        pen.setColor(QColor(0xEE, 0x22, 0x22, 0xEE));
        painter.setPen(pen);
        brush.setColor(QColor(0xEE, 0x22, 0x22, 0xEE));
        painter.setBrush(brush);


        painter.drawEllipse(pos-5, 20, 10, 10);

    }
}


void sliderProgress::mouseMoveEvent(QMouseEvent *event)
{
    (void) event;

    isShow = 1;
    double pos = value*(this->width()-20)/(maximum-minimum)+10;
    int currentValue = (event->x()-10)*(maximum-minimum)/(this->width()-20) + minimum;  //在当前鼠标位置对应的值

    if(event->x() > pos-10 && event->x() < pos+10
            && event->y() > 15 && event->y() < 35)
    {
        this->setCursor(Qt::PointingHandCursor);
    }
    else
    {
        this->setCursor(Qt::ArrowCursor);
    }

    if(isPointed == 1 && currentValue >= minimum && currentValue <= maximum)
    {
        value = currentValue;
        if(tracking == true)            //由tracking控制是否每次value发生改变都发出valueChange(int)信号
            emit valueChanged(value);
        emit valueChangedTracking(value);
    }
    update();
}

void sliderProgress::mousePressEvent(QMouseEvent *event)
{
    (void) event;

    double pos = value*(this->width()-20)/(maximum-minimum)+10;
    int currentValue = (event->x()-10)*(maximum-minimum)/(this->width()-20) + minimum;
    if(event->y() > 15 && event->y() < 35
            && (event->x() < pos-10 || event->x() > pos+10)
            && currentValue >= minimum && currentValue <= maximum)
    {
        value = currentValue;
        emit valueChanged(value);
        emit valueChangedTracking(value);
    }
    else if(event->y() > 15 && event->y() < 35
            && (event->x() > pos-10 || event->x() < pos+10))
    {
        isPointed = 1;
        allowSetValue = 0;
    }

    update();
}


void sliderProgress::mouseReleaseEvent(QMouseEvent *event)
{
    (void) event;

    if(isPointed == 1)
    {
        emit valueChanged(value);
        emit valueChangedTracking(value);
    }
    isPointed = 0;
    allowSetValue = 1;
}


void sliderProgress::enterEvent(QEvent *event)
{
    (void) event;

    isShow = 1;
}
void sliderProgress::leaveEvent(QEvent *event)
{
    (void) event;

    if(isAlwaysShow == 0)
    {
        isShow = 0;
    }
    update();
}



void sliderProgress::setMinimum(int n)
{
    minimum = n;
}
void sliderProgress::setMaximum(int n)
{
    maximum = n;
}
void sliderProgress::setTracking(bool s)
{
    tracking = s;
}

int sliderProgress::getMinimum()
{
    return minimum;
}
int sliderProgress::getMaximum()
{
    return maximum;
}
bool sliderProgress::hasTracking()
{
    return tracking;
}

void sliderProgress::setValue(int v)
{
    if(allowSetValue == 1)
    {
        value = v;
        emit valueChangedTracking(value);
    }
    //emit valueChanged(value);
    update();
}
void sliderProgress::setRange(int min, int max)
{
    minimum = min;
    maximum = max;
    if(value > maximum)
    {
        value = maximum;
        emit valueChangedTracking(value);
    }
    emit rangeChange(min, max);
    update();
}


void sliderProgress::setIsAlwaysShow(int s)
{
    isAlwaysShow = s;
    isShow = 0;
    update();
}
